Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 632)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.02791 13.02450 13.02116 13.01790 13.01470 13.01156 13.00848 13.00545
##   [9] 13.00246 12.99950 12.99658 12.99369 12.99082 12.98796 12.98512 12.98228
##  [17] 12.97944 12.97659 12.97374 12.97086 12.96797 12.96505 12.96209 12.95910
##  [25] 12.95606 12.95297 12.94982 12.94662 12.94335 12.94003 12.93668 12.93331
##  [33] 12.92992 12.92651 12.92308 12.91965 12.91622 12.91279 12.90936 12.90594
##  [41] 12.90253 12.89914 12.89578 12.89243 12.88912 12.88584 12.88260 12.87940
##  [49] 12.87624 12.87314 12.87009 12.86710 12.86417 12.86131 12.85852 12.85580
##  [57] 12.85316 12.85061 12.84814 12.84577 12.84349 12.84131 12.83924 12.83728
##  [65] 12.83532 12.83328 12.83116 12.82898 12.82674 12.82445 12.82212 12.81975
##  [73] 12.81736 12.81496 12.81255 12.81015 12.80775 12.80538 12.80303 12.80073
##  [81] 12.79846 12.79626 12.79412 12.79205 12.79006 12.78817 12.78637 12.78468
##  [89] 12.78311 12.78166 12.78035 12.77918 12.77816 12.77730 12.77661 12.77610
##  [97] 12.77577 12.77564 12.77572 12.77579 12.77565 12.77533 12.77483 12.77418
## [105] 12.77341 12.77251 12.77152 12.77046 12.76934 12.76818 12.76700 12.76582
## [113] 12.76465 12.76352 12.76245 12.76145 12.76054 12.75975 12.75908 12.75856
## [121] 12.75821 12.75804 12.75808 12.75834 12.75885 12.75961 12.76066 12.76200
## [129] 12.76366 12.76566 12.76801 12.77073 12.77385 12.77840 12.78521 12.79398
## [137] 12.80441 12.81621 12.82906 12.84268 12.85676 12.87101 12.88511 12.89877
## [145] 12.91170 12.92358 12.93413 12.94304 12.95001 12.95728 12.96708 12.97913
## [153] 12.99311 13.00873 13.02568 13.04367 13.06239 13.08154 13.10083 13.11995
## [161] 13.13859 13.15647 13.17328 13.18871 13.20247 13.21426 13.22378 13.23072
## [169] 13.23639 13.24230 13.24840 13.25465 13.26104 13.26751 13.27405 13.28061
## [177] 13.28715 13.29366 13.30009 13.30640 13.31257 13.31856 13.32434 13.32987
## [185] 13.33512 13.34005 13.34464 13.34884 13.35263 13.35596 13.35881 13.36114
## [193] 13.36293 13.36412 13.36469 13.36461 13.36385 13.36236 13.36011 13.35707
## [201] 13.35322 13.34850 13.34289 13.33636 13.32887 13.32038 13.31087 13.30030
## [209] 13.28863 13.27583 13.26044 13.24132 13.21896 13.19382 13.16638 13.13711
## [217] 13.10648 13.07498 13.04307 13.01123 12.97993 12.94965 12.92086 12.89404
## [225] 12.86965 12.84817 12.82632 12.80076 12.77193 12.74028 12.70624 12.67025
## [233] 12.63276 12.59419 12.55500 12.51562 12.47648 12.43803 12.40071 12.36496
## [241] 12.33121 12.29990 12.27148 12.24638 12.22504 12.20608 12.18779 12.17014
## [249] 12.15307 12.13655 12.12053 12.10499 12.08986 12.07511 12.06071 12.04660
## [257] 12.03275 12.01912 12.00565 11.99232 11.97908 11.96728 11.95812 11.95133
## [265] 11.94662 11.94372 11.94235 11.94223 11.94309 11.94464 11.94662 11.94874
## [273] 11.95073 11.95230 11.95319 11.95311 11.95179 11.94895 11.94431 11.93759
## [281] 11.93039 11.92437 11.91936 11.91518 11.91167 11.90866 11.90597 11.90344
## [289] 11.90090 11.89817 11.89508 11.89147 11.88716 11.88198 11.87577 11.86834
## [297] 11.86009 11.85152 11.84267 11.83358 11.82429 11.81483 11.80523 11.79554
## [305] 11.78579 11.77601 11.76625 11.75654 11.74691 11.73741 11.72807 11.71892
## [313] 11.71000 11.70135 11.69301 11.68388 11.67295 11.66038 11.64633 11.63093
## [321] 11.61436 11.59676 11.57828 11.55909 11.53932 11.51915 11.49871 11.47817
## [329] 11.45768 11.43739 11.41746 11.39803 11.37927 11.36132 11.34435 11.32849
## [337] 11.31392 11.30078 11.28922 11.27940 11.27148 11.26560 11.26192 11.26060
## [345] 11.26178 11.26489 11.26919 11.27463 11.28114 11.28867 11.29716 11.30653
## [353] 11.31674 11.32771 11.33940 11.35173 11.36466 11.37811 11.39202 11.40634
## [361] 11.42101 11.43596 11.45113 11.46646 11.48390 11.50518 11.52992 11.55776
## [369] 11.58833 11.62126 11.65616 11.69268 11.73044 11.76907 11.80819 11.84744
## [377] 11.88644 11.92483 11.96222 11.99826 12.03257 12.06477 12.09449 12.12137
## [385] 12.14504 12.16923 12.19754 12.22933 12.26398 12.30085 12.33931 12.37873
## [393] 12.41850 12.45796 12.49651 12.53350 12.56830 12.60030 12.62885 12.65333
## [401] 12.67311 12.69076 12.70916 12.72815 12.74757 12.76725 12.78704 12.80678
## [409] 12.82630 12.84545 12.86405 12.88196 12.89901 12.91504 12.92989 12.94339
## [417] 12.95539 12.96573 12.97424 12.98077 12.98456 12.98522 12.98311 12.97859
## [425] 12.97203 12.96377 12.95420 12.94366 12.93252 12.92114 12.90988 12.89911
## [433] 12.88919 12.88047 12.87331 12.86809 12.86227 12.85329 12.84149 12.82722
## [441] 12.81081 12.79261 12.77294 12.75215 12.73059 12.70857 12.68646 12.66458
## [449] 12.64327 12.62288 12.60374 12.58619 12.57057 12.55722 12.54647 12.53657
## [457] 12.52560 12.51364 12.50079 12.48713 12.47276 12.45777 12.44225 12.42629
## [465] 12.40999 12.39342 12.37669 12.35989 12.34310 12.32642 12.30994 12.29375
## [473] 12.27794 12.26260 12.24782 12.23370 12.22032 12.20778 12.19617 12.18557
## [481] 12.17609 12.16780 12.16081 12.15520 12.15106 12.14809 12.14590 12.14443
## [489] 12.14366 12.14355 12.14406 12.14515 12.14678 12.14891 12.15151 12.15454
## [497] 12.15796 12.16174 12.16582 12.17018 12.17478 12.17958 12.18454 12.18962
## [505] 12.19613 12.20524 12.21667 12.23017 12.24548 12.26233 12.28046 12.29962
## [513] 12.31952 12.33993 12.36057 12.38117 12.40149 12.42126 12.44021 12.45808
## [521] 12.47462 12.48955 12.50262 12.51356 12.52212 12.53070 12.54170 12.55481
## [529] 12.56976 12.58624 12.60396 12.62263 12.64196 12.66165 12.68142 12.70096
## [537] 12.71999 12.73821 12.75533 12.77105 12.78510 12.79716 12.80695 12.81417
## [545] 12.81854 12.81976 12.81872 12.81654 12.81330 12.80904 12.80385 12.79777
## [553] 12.79087 12.78322 12.77488 12.76591 12.75637 12.74633 12.73586 12.72500
## [561] 12.71384 12.70242 12.69082 12.67910 12.66732 12.65554 12.64383 12.63182
## [569] 12.61913 12.60579 12.59182 12.57726 12.56213 12.54646 12.53028 12.51361
## [577] 12.49648 12.47893 12.46097 12.44264 12.42397 12.40498 12.38570 12.36604
## [585] 12.34589 12.32525 12.30411 12.28248 12.26036 12.23775 12.21465 12.19106
## [593] 12.16699 12.14243 12.11738 12.09184 12.06582 12.03932 12.01233 11.98486
## [601] 11.95691 11.92847 11.89957 11.87020 11.84039 11.81011 11.77938 11.74819
## [609] 11.71655 11.68444 11.65188 11.61886 11.58537 11.55143 11.51703 11.48217
## [617] 11.44684 11.41106 11.37481 11.33810 11.30093 11.26330 11.22520 11.18664
## [625] 11.14762 11.10813 11.06818 11.02776 10.98688 10.94553 10.90371 10.86143
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 632)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.63151 12.62670 12.62200 12.61741 12.61293 12.60855 12.60428 12.60010
##   [9] 12.59601 12.59202 12.58812 12.58430 12.58056 12.57690 12.57332 12.56982
##  [17] 12.56638 12.56301 12.55970 12.55646 12.55327 12.55014 12.54705 12.54402
##  [25] 12.54103 12.53809 12.53518 12.53231 12.52948 12.52668 12.52392 12.52120
##  [33] 12.51853 12.51592 12.51335 12.51085 12.50840 12.50601 12.50369 12.50144
##  [41] 12.49926 12.49715 12.49512 12.49317 12.49130 12.48952 12.48782 12.48622
##  [49] 12.48471 12.48330 12.48198 12.48077 12.47967 12.47868 12.47779 12.47702
##  [57] 12.47637 12.47584 12.47544 12.47516 12.47500 12.47499 12.47510 12.47535
##  [65] 12.47573 12.47621 12.47680 12.47749 12.47829 12.47919 12.48020 12.48131
##  [73] 12.48253 12.48386 12.48529 12.48683 12.48847 12.49022 12.49207 12.49404
##  [81] 12.49611 12.49828 12.50056 12.50295 12.50545 12.50805 12.51076 12.51358
##  [89] 12.51650 12.51953 12.52267 12.52592 12.52927 12.53274 12.53631 12.53999
##  [97] 12.54377 12.54767 12.55167 12.55580 12.56008 12.56450 12.56905 12.57372
## [105] 12.57851 12.58342 12.58843 12.59353 12.59873 12.60401 12.60936 12.61479
## [113] 12.62028 12.62582 12.63141 12.63704 12.64270 12.64840 12.65411 12.65983
## [121] 12.66556 12.67130 12.67702 12.68273 12.68841 12.69407 12.69969 12.70527
## [129] 12.71134 12.71837 12.72625 12.73488 12.74415 12.75394 12.76416 12.77469
## [137] 12.78543 12.79626 12.80709 12.81779 12.82828 12.83843 12.84813 12.85729
## [145] 12.86580 12.87353 12.88040 12.88870 12.90050 12.91529 12.93258 12.95188
## [153] 12.97269 12.99451 13.01685 13.03922 13.06111 13.08204 13.10151 13.11903
## [161] 13.13409 13.14621 13.15488 13.16247 13.17156 13.18198 13.19359 13.20623
## [169] 13.21973 13.23394 13.24871 13.26386 13.27926 13.29473 13.31012 13.32528
## [177] 13.34004 13.35425 13.36775 13.38038 13.39198 13.40240 13.41149 13.41907
## [185] 13.42499 13.42911 13.43125 13.43126 13.42898 13.42448 13.41801 13.40972
## [193] 13.39976 13.38828 13.37541 13.36131 13.34613 13.33001 13.31309 13.29553
## [201] 13.27747 13.25905 13.24043 13.22174 13.20314 13.18478 13.16679 13.14933
## [209] 13.13254 13.11656 13.09857 13.07603 13.04957 13.01982 12.98741 12.95296
## [217] 12.91711 12.88047 12.84367 12.80735 12.77212 12.73863 12.70748 12.67931
## [225] 12.65475 12.63443 12.61512 12.59340 12.56957 12.54393 12.51679 12.48847
## [233] 12.45925 12.42945 12.39937 12.36931 12.33959 12.31051 12.28237 12.25547
## [241] 12.23013 12.20664 12.18532 12.16646 12.15038 12.13658 12.12425 12.11325
## [249] 12.10342 12.09459 12.08661 12.07934 12.07260 12.06624 12.06012 12.05406
## [257] 12.04791 12.04153 12.03474 12.02740 12.01935 12.01256 12.00890 12.00802
## [265] 12.00957 12.01323 12.01864 12.02545 12.03334 12.04195 12.05095 12.05999
## [273] 12.06872 12.07682 12.08392 12.08970 12.09380 12.09589 12.09562 12.09265
## [281] 12.08914 12.08732 12.08692 12.08770 12.08939 12.09174 12.09451 12.09742
## [289] 12.10022 12.10267 12.10450 12.10546 12.10530 12.10375 12.10057 12.09549
## [297] 12.08882 12.08109 12.07240 12.06287 12.05261 12.04171 12.03029 12.01846
## [305] 12.00632 11.99398 11.98155 11.96913 11.95684 11.94478 11.93306 11.92178
## [313] 11.91106 11.90100 11.89170 11.88173 11.86966 11.85567 11.83993 11.82262
## [321] 11.80390 11.78394 11.76292 11.74102 11.71839 11.69521 11.67166 11.64791
## [329] 11.62412 11.60047 11.57713 11.55427 11.53206 11.51068 11.49029 11.47108
## [337] 11.45320 11.43683 11.42215 11.40931 11.39851 11.38990 11.38366 11.37996
## [345] 11.37897 11.38002 11.38228 11.38567 11.39014 11.39561 11.40203 11.40932
## [353] 11.41742 11.42627 11.43579 11.44592 11.45661 11.46777 11.47935 11.49127
## [361] 11.50348 11.51591 11.52849 11.54115 11.55574 11.57393 11.59535 11.61967
## [369] 11.64655 11.67564 11.70659 11.73906 11.77270 11.80718 11.84213 11.87724
## [377] 11.91213 11.94648 11.97993 12.01215 12.04278 12.07148 12.09792 12.12173
## [385] 12.14258 12.16379 12.18853 12.21629 12.24653 12.27872 12.31234 12.34686
## [393] 12.38174 12.41646 12.45048 12.48329 12.51434 12.54311 12.56908 12.59170
## [401] 12.61046 12.62776 12.64623 12.66568 12.68589 12.70669 12.72786 12.74922
## [409] 12.77057 12.79170 12.81242 12.83254 12.85186 12.87017 12.88729 12.90301
## [417] 12.91714 12.92948 12.93983 12.94800 12.95454 12.96018 12.96493 12.96885
## [425] 12.97195 12.97428 12.97587 12.97676 12.97697 12.97654 12.97551 12.97391
## [433] 12.97177 12.96913 12.96602 12.96248 12.95677 12.94739 12.93474 12.91920
## [441] 12.90116 12.88100 12.85910 12.83587 12.81167 12.78691 12.76196 12.73721
## [449] 12.71305 12.68986 12.66804 12.64796 12.63002 12.61460 12.60209 12.59030
## [457] 12.57687 12.56195 12.54567 12.52816 12.50956 12.49001 12.46964 12.44858
## [465] 12.42699 12.40498 12.38270 12.36028 12.33786 12.31557 12.29355 12.27194
## [473] 12.25087 12.23048 12.21090 12.19227 12.17472 12.15840 12.14344 12.12996
## [481] 12.11812 12.10804 12.09987 12.09373 12.08976 12.08712 12.08490 12.08308
## [489] 12.08170 12.08074 12.08024 12.08019 12.08060 12.08149 12.08286 12.08472
## [497] 12.08709 12.08997 12.09337 12.09731 12.10179 12.10682 12.11241 12.11858
## [505] 12.12680 12.13835 12.15290 12.17013 12.18973 12.21136 12.23471 12.25946
## [513] 12.28528 12.31185 12.33885 12.36596 12.39286 12.41923 12.44474 12.46907
## [521] 12.49190 12.51292 12.53179 12.54819 12.56182 12.57600 12.59400 12.61539
## [529] 12.63971 12.66653 12.69541 12.72590 12.75755 12.78994 12.82261 12.85512
## [537] 12.88704 12.91791 12.94730 12.97477 12.99987 13.02215 13.04119 13.05653
## [545] 13.06774 13.07436 13.07808 13.08087 13.08274 13.08373 13.08385 13.08312
## [553] 13.08156 13.07920 13.07604 13.07212 13.06745 13.06205 13.05595 13.04915
## [561] 13.04170 13.03359 13.02486 13.01553 13.00561 12.99512 12.98409 12.97240
## [569] 12.95990 12.94662 12.93256 12.91774 12.90216 12.88585 12.86879 12.85102
## [577] 12.83254 12.81336 12.79349 12.77294 12.75172 12.72985 12.70734 12.68415
## [585] 12.66026 12.63567 12.61037 12.58436 12.55763 12.53018 12.50202 12.47313
## [593] 12.44351 12.41317 12.38209 12.35028 12.31773 12.28443 12.25039 12.21560
## [601] 12.18007 12.14377 12.10675 12.06900 12.03054 11.99136 11.95146 11.91085
## [609] 11.86952 11.82748 11.78472 11.74124 11.69704 11.65213 11.60651 11.56017
## [617] 11.51311 11.46533 11.41684 11.36764 11.31772 11.26708 11.21573 11.16366
## [625] 11.11088 11.05738 11.00317 10.94824 10.89259 10.83623 10.77916 10.72137
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 632)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.02796 12.02285 12.01786 12.01299 12.00823 12.00357 11.99901 11.99455
##   [9] 11.99017 11.98587 11.98165 11.97750 11.97341 11.96939 11.96541 11.96148
##  [17] 11.95759 11.95374 11.94992 11.94612 11.94233 11.93856 11.93480 11.93104
##  [25] 11.92727 11.92349 11.91970 11.91588 11.91204 11.90816 11.90424 11.90028
##  [33] 11.89627 11.89220 11.88806 11.88386 11.87962 11.87537 11.87111 11.86686
##  [41] 11.86262 11.85839 11.85418 11.85000 11.84585 11.84173 11.83766 11.83364
##  [49] 11.82967 11.82576 11.82191 11.81814 11.81444 11.81082 11.80729 11.80386
##  [57] 11.80052 11.79729 11.79417 11.79117 11.78828 11.78553 11.78290 11.78042
##  [65] 11.77808 11.77589 11.77385 11.77198 11.77027 11.76873 11.76737 11.76596
##  [73] 11.76428 11.76235 11.76019 11.75784 11.75530 11.75261 11.74980 11.74687
##  [81] 11.74386 11.74080 11.73770 11.73458 11.73148 11.72841 11.72541 11.72248
##  [89] 11.71966 11.71697 11.71444 11.71208 11.70992 11.70799 11.70631 11.70490
##  [97] 11.70378 11.70298 11.70253 11.70244 11.70274 11.70345 11.70461 11.70622
## [105] 11.70832 11.71093 11.71400 11.71746 11.72131 11.72553 11.73011 11.73504
## [113] 11.74029 11.74586 11.75175 11.75792 11.76437 11.77110 11.77808 11.78530
## [121] 11.79275 11.80041 11.80828 11.81634 11.82458 11.83299 11.84155 11.85025
## [129] 11.85907 11.86801 11.87705 11.88618 11.89539 11.90637 11.92056 11.93754
## [137] 11.95689 11.97819 12.00103 12.02499 12.04964 12.07458 12.09937 12.12361
## [145] 12.14688 12.16875 12.18882 12.20665 12.22184 12.23762 12.25720 12.28015
## [153] 12.30600 12.33432 12.36464 12.39653 12.42953 12.46318 12.49706 12.53069
## [161] 12.56364 12.59545 12.62568 12.65387 12.67958 12.70236 12.72175 12.73731
## [169] 12.75069 12.76380 12.77664 12.78917 12.80138 12.81325 12.82476 12.83587
## [177] 12.84658 12.85686 12.86670 12.87606 12.88493 12.89328 12.90110 12.90837
## [185] 12.91505 12.92114 12.92661 12.93144 12.93561 12.93909 12.94187 12.94392
## [193] 12.94522 12.94576 12.94551 12.94444 12.94255 12.93980 12.93617 12.93165
## [201] 12.92622 12.91984 12.91251 12.90419 12.89487 12.88453 12.87315 12.86070
## [209] 12.84716 12.83251 12.81425 12.79037 12.76165 12.72885 12.69272 12.65403
## [217] 12.61353 12.57200 12.53019 12.48886 12.44877 12.41069 12.37537 12.34359
## [225] 12.31609 12.29364 12.27252 12.24875 12.22263 12.19451 12.16471 12.13357
## [233] 12.10140 12.06854 12.03531 12.00205 11.96909 11.93675 11.90537 11.87526
## [241] 11.84677 11.82021 11.79592 11.77423 11.75547 11.73896 11.72373 11.70964
## [249] 11.69656 11.68434 11.67284 11.66194 11.65148 11.64133 11.63136 11.62142
## [257] 11.61137 11.60109 11.59042 11.57923 11.56739 11.55648 11.54803 11.54178
## [265] 11.53746 11.53481 11.53358 11.53350 11.53431 11.53575 11.53756 11.53949
## [273] 11.54125 11.54261 11.54330 11.54305 11.54160 11.53870 11.53409 11.52750
## [281] 11.52047 11.51463 11.50980 11.50583 11.50256 11.49983 11.49748 11.49535
## [289] 11.49329 11.49112 11.48870 11.48586 11.48245 11.47829 11.47325 11.46714
## [297] 11.45983 11.45134 11.44181 11.43139 11.42021 11.40840 11.39611 11.38347
## [305] 11.37062 11.35770 11.34485 11.33219 11.31988 11.30805 11.29684 11.28637
## [313] 11.27680 11.26826 11.26089 11.25337 11.24439 11.23408 11.22258 11.21002
## [321] 11.19653 11.18224 11.16728 11.15179 11.13590 11.11974 11.10345 11.08716
## [329] 11.07099 11.05509 11.03958 11.02459 11.01027 10.99674 10.98413 10.97258
## [337] 10.96221 10.95317 10.94558 10.93958 10.93530 10.93287 10.93242 10.93409
## [345] 10.93801 10.94435 10.95304 10.96389 10.97669 10.99122 11.00727 11.02464
## [353] 11.04312 11.06250 11.08257 11.10313 11.12395 11.14484 11.16558 11.18597
## [361] 11.20580 11.22486 11.24294 11.25982 11.27756 11.29814 11.32128 11.34671
## [369] 11.37412 11.40325 11.43382 11.46553 11.49812 11.53129 11.56477 11.59827
## [377] 11.63151 11.66422 11.69610 11.72687 11.75626 11.78398 11.80976 11.83330
## [385] 11.85432 11.87526 11.89847 11.92360 11.95028 11.97814 12.00682 12.03595
## [393] 12.06518 12.09414 12.12246 12.14978 12.17573 12.19995 12.22208 12.24175
## [401] 12.25860 12.27423 12.29043 12.30706 12.32401 12.34115 12.35837 12.37555
## [409] 12.39255 12.40927 12.42558 12.44135 12.45648 12.47083 12.48429 12.49673
## [417] 12.50803 12.51808 12.52675 12.53392 12.53946 12.54342 12.54597 12.54726
## [425] 12.54745 12.54669 12.54513 12.54293 12.54026 12.53725 12.53408 12.53089
## [433] 12.52785 12.52510 12.52280 12.52111 12.51858 12.51380 12.50702 12.49844
## [441] 12.48829 12.47681 12.46422 12.45075 12.43661 12.42204 12.40727 12.39251
## [449] 12.37800 12.36397 12.35063 12.33821 12.32695 12.31706 12.30878 12.30073
## [457] 12.29145 12.28105 12.26961 12.25722 12.24399 12.22999 12.21532 12.20008
## [465] 12.18436 12.16825 12.15185 12.13524 12.11851 12.10177 12.08511 12.06861
## [473] 12.05237 12.03648 12.02104 12.00614 11.99186 11.97831 11.96557 11.95375
## [481] 11.94292 11.93318 11.92464 11.91737 11.91147 11.90600 11.90001 11.89364
## [489] 11.88698 11.88015 11.87326 11.86642 11.85976 11.85337 11.84737 11.84188
## [497] 11.83701 11.83286 11.82956 11.82721 11.82593 11.82583 11.82702 11.82962
## [505] 11.83395 11.84016 11.84807 11.85752 11.86835 11.88039 11.89346 11.90740
## [513] 11.92205 11.93724 11.95279 11.96855 11.98434 11.99999 12.01535 12.03024
## [521] 12.04450 12.05795 12.07043 12.08178 12.09182 12.10287 12.11712 12.13422
## [529] 12.15381 12.17552 12.19901 12.22393 12.24990 12.27658 12.30362 12.33064
## [537] 12.35731 12.38326 12.40813 12.43158 12.45324 12.47276 12.48978 12.50394
## [545] 12.51490 12.52229 12.52737 12.53167 12.53521 12.53805 12.54020 12.54172
## [553] 12.54263 12.54298 12.54279 12.54211 12.54096 12.53940 12.53745 12.53514
## [561] 12.53253 12.52963 12.52650 12.52315 12.51964 12.51600 12.51226 12.50816
## [569] 12.50344 12.49814 12.49229 12.48593 12.47910 12.47183 12.46416 12.45613
## [577] 12.44777 12.43912 12.43022 12.42110 12.41180 12.40235 12.39280 12.38305
## [585] 12.37296 12.36254 12.35178 12.34067 12.32921 12.31740 12.30523 12.29270
## [593] 12.27979 12.26651 12.25285 12.23881 12.22438 12.20955 12.19432 12.17870
## [601] 12.16266 12.14621 12.12937 12.11218 12.09464 12.07675 12.05850 12.03991
## [609] 12.02096 12.00165 11.98200 11.96199 11.94163 11.92091 11.89984 11.87842
## [617] 11.85665 11.83452 11.81203 11.78920 11.76601 11.74246 11.71856 11.69431
## [625] 11.66970 11.64474 11.61942 11.59375 11.56773 11.54135 11.51461 11.48752
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")